package com.itheima.controller;

//检查组

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/CheckGroup")
public class CheckGroupController {


    @Reference
    private CheckGroupService checkGroupService;

    //查询所有检查组
    @RequestMapping("/queryCheckItem")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result queryCheckItem() {
        List<CheckItem> checkItems = checkGroupService.queryCheckItem();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
    }


    //添加检查组
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
//            添加成功
            checkGroupService.add(checkGroup, checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }


    //查询所有检查组
    @RequestMapping("/queryAll")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result queryAll(@RequestBody QueryPageBean queryPageBean) {

        try {
            Integer currentPage = queryPageBean.getCurrentPage();//当前页数
            Integer pageSize = queryPageBean.getPageSize();//每页显示的条数
            String queryString = queryPageBean.getQueryString();//查询条件
            //System.out.println(queryPageBean);

            PageResult pageResult = checkGroupService.queryAll(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            //System.out.println(pageResult);
            Long total = pageResult.getTotal(); //总记录数
            int _currentPage =(int) Math.ceil((total*1.0) / (pageSize*1.0));//总页数
//            System.out.println("总记录数"+total);
//            System.out.println("数据库查出真实的总页数"+_currentPage);
//            System.out.println("要查的当前页数"+currentPage);
            /*
             * _currentPage  当前数据库查出的真实的总页数
             * currentPage   前端传过来的要查的当前页码
             * 如果要查的当前页码大于真实的总页数,那么就需要把当前页码的值赋值给总页数进行重新查询并返回
             * */
            if(currentPage>_currentPage){
                PageResult querySetmealAll = checkGroupService.queryAll(_currentPage, pageSize, queryString);
                return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,querySetmealAll);
            }
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

//            return new Result(true,"查询成功",pageResult);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false,"网络异常,请重新刷新页面");
//        }

    }


    //编辑检查组中(根据ID查询检查组 ; 回显普通数据)

    @RequestMapping("/queryById")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result queryById(Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.queryById(id);
            return new Result(true,"初始化检查组成功",checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"网络异常,请重新刷新页面");
        }
    }

    //编辑检查组中(根据ID查询中间表 ; 回显该检查组中包含的检查项的ID值)
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @RequestMapping("/checkitemIds")
    public Result checkitemIds(Integer id){
        try {
            List<Integer> checkitemIds = checkGroupService.checkitemByIds(id);
           // System.out.println(checkitemIds);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

//    修改编辑检查组
    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody CheckGroup checkGroup ,Integer [] checkitemIds){
        //System.out.println(checkGroup);
        //System.out.println(checkitemIds);

        try {
            checkGroupService.update(checkGroup,checkitemIds);
            //修改成功
           return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return new Result(true,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    //删除检查组(根据ID删除)
    @RequestMapping("/DeleteById")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result DeleteById(Integer id){
        try {
            boolean b = checkGroupService.DeleteById(id);
            if(b){
                return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false,"删除失败!该检查组已被其他检查套餐引用");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器异常");
        }
    }

}
